<template>
  <a-breadcrumb class="container-breadcrumb">
    <a-breadcrumb-item>
      <icon-apps />
    </a-breadcrumb-item>
    <a-breadcrumb-item v-for="item in items" :key="item">
      {{ item.title }}
    </a-breadcrumb-item>
  </a-breadcrumb>
</template>

<script lang="ts" setup>
  import { computed } from 'vue';
  import { useRoute } from 'vue-router';

  const items = computed(() => {
    const route = useRoute();
    const pathArray = route.path.split('/');
    pathArray.shift();

    const breadcrumbs = pathArray.reduce(
      (breadcrumbArray: any[], path, idx) => {
        // 如果路由下有hiddenBreadcrumb或当前遍历到参数则隐藏
        if (
          route.matched[idx]?.meta?.hiddenBreadcrumb ||
          Object.values(route.params).includes(path)
        ) {
          return breadcrumbArray;
        }

        breadcrumbArray.push({
          path,
          to: breadcrumbArray[idx - 1]
            ? `/${breadcrumbArray[idx - 1].path}/${path}`
            : `/${path}`,
          title: route.matched[idx]?.meta?.title ?? path,
        });
        return breadcrumbArray;
      },
      []
    );
    return breadcrumbs;
  });
</script>

<style scoped lang="less">
  .container-breadcrumb {
    margin: 16px 0;
    :deep(.arco-breadcrumb-item) {
      color: rgb(var(--gray-6));
      &:last-child {
        color: rgb(var(--gray-8));
      }
    }
  }
</style>
